﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.DAL;
using SeDuce.BLL;
using SeDuce.Model;

public partial class Management_Articles_List : PagerBase
{
    Lazy_Yu<ArticleBLL> lazyArticleBLL = new Lazy_Yu<ArticleBLL>(() => new ArticleBLL());
    public ArticleBLL LazyArticleBLL
    {
        get { return this.lazyArticleBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.RegistrPager(this.GridView1, this.rptPageFliper, 7, this.FillPageMoudle(20, SeDuce.DAL.ArticleDAL.sqlGetArticles, new List<SortField>() { 
            new SortField() { FieldName = "ID", DESC = false }
        }));
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow row = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
        string strID = ((Label)this.GridView1.Rows[row.RowIndex].Cells[1].FindControl("lblID")).Text.Trim();
        Article article = LazyArticleBLL.GetArticleByID(strID.ToInt()).ToList<Article>().SingleOrDefault();
        if (e.CommandName == "MOD")
        {
            try
            {
                string strTitle = ((Label)this.GridView1.Rows[row.RowIndex].Cells[2].FindControl("lblTitle")).Text.Trim();
                string strTypeID = ((Label)this.GridView1.Rows[row.RowIndex].Cells[3].FindControl("hidTypeID")).Text;
                this.lblID.Text = strID;
                this.txtTitle.Text = strTitle;
                this.txtContent.Value = article.ContentText;
                this.DropDownListFixByValue(this.ddlType, strTypeID);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key5", "javascript:InitEditor();", true);
            }
            catch { }
        }
        else if (e.CommandName == "DEL")
        {
            this.LazyArticleBLL.DeleteArticle(strID.ToInt());
            this.BindPage(this.rptPageFliper.UniqueID, 1);
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key5", "javascript:InitEditor();", true);
        }
    }

    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.lblID.Text.Trim()))
        {
            //ADD
            if (this.LazyArticleBLL.InsertArticle(new SeDuce.Model.Article() {  Title = this.txtTitle.Text.Trim(), ContentText = this.txtContent.Value.Trim(), Type = this.ddlType.SelectedValue.ToInt() }))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key1", "javascript:" + string.Format("alert('{0}');window.location.reload();", "操作成功!"), true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
            }
        }
        else
        {
            //EDIT
            if (this.LazyArticleBLL.UpdateArticle(new SeDuce.Model.Article() { ID = this.lblID.Text.Trim().ToInt(), Title = this.txtTitle.Text.Trim(), ContentText = this.txtContent.Value.Trim(), Type = this.ddlType.SelectedValue.ToInt() }))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key3", "javascript:" + string.Format("alert('{0}');window.location.reload();", "操作成功!"), true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key4", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
            }
        }
    }
}